package com.wl.cloud.security.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author: wanglin
 * @date: 2023-09-18 周一
 * @Version: 1.0
 * @Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUserDetails implements UserDetails {

    private String nickname;
    private String username;
    private String password;
    /**
     * 用户唯一标识:UUID生成的
     */
    private String token;
    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    /**
     * 角色列表
     */
    private Set<String> roles;

    /**
     * 是否退出登录
     */
    private Boolean isLogOut;
    //实现类 SimpleGrantedAuthority
//    private Collection<? extends GrantedAuthority> authorities;
    private List<GrantedAuthority> authorities;
    /**
     * 只要有一个为false，就不可用用
     */
    private Boolean isAccountNonExpired;
    private Boolean isAccountNonLocked;
    private Boolean isCredentialsNonExpired;
    private Boolean isEnabled;


    public LoginUserDetails(List<GrantedAuthority> authorities) {
        this.isLogOut = Boolean.FALSE;
        this.authorities = authorities;
    }

    public LoginUserDetails(String username, List<GrantedAuthority> authorities) {
        this.username = username;
        this.isLogOut = Boolean.FALSE;
        this.authorities = authorities;
        this.isEnabled = Boolean.FALSE;
        this.isAccountNonExpired = Boolean.FALSE;
        this.isAccountNonLocked = Boolean.FALSE;
        this.isCredentialsNonExpired = Boolean.FALSE;
    }

    public LoginUserDetails(String username, String password, List<GrantedAuthority> authorities) {
        this.username = username;
        this.password = password;
        this.isLogOut = Boolean.FALSE;
        this.authorities = authorities;
        this.isEnabled = Boolean.FALSE;
        this.isAccountNonExpired = Boolean.FALSE;
        this.isAccountNonLocked = Boolean.FALSE;
        this.isCredentialsNonExpired = Boolean.FALSE;
    }
// public LoginUserDetails(SysUser user, List<GrantedAuthority> authorities) {
//        this.username = user.getUserId();
//        this.password = user.getPassword();
//        this.isLogOut = Boolean.FALSE;
//        this.authorities = authorities;
//        this.isEnabled = user.getIsEnabled();
//        this.isAccountNonExpired = user.getIsAccountNonExpired();
//        this.isAccountNonLocked = user.getIsAccountNonLocked();
//        this.isCredentialsNonExpired = user.getIsCredentialsNonExpired();
//    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
